<?php

namespace App\Http\Controllers\Api\Orders;

use App\Http\Controllers\Controller;
use App\Http\Requests\Orders\BookTicketRequest;
use App\Http\Resources\Trip\Orders\OrderResource;
use App\Http\Resources\TripBookOrderResource;
use App\Models\Orders\Repositories\Interfaces\OrdersRepositoryInterface;
use App\Trip\Orders\BookOrders\BookOrdersManager;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;

class OrdersController extends Controller
{
    private OrdersRepositoryInterface $orderRepo;
    private string|null               $user_id;

    public function __construct(
        OrdersRepositoryInterface $ordersRepo
    )
    {
        $this->orderRepo = $ordersRepo;
        $this->user_id   = auth()->id();
    }

    public function index_query(Request $request)
    {
        $request->offsetSet('index_query', 1);
        $res           = $this->orderRepo->lists([
            'index_query' => 1,
            'user_id'     => $this->user_id,
        ],
            5);
        $res['orders'] = OrderResource::collection($res['orders']);
        return $this->success('', $res);
    }

    public function all_orders(Request $request)
    {
        $request->offsetSet('index_query', 0);
        $params            = $request->only([
            'sort_key',
            'sort_value',
            'is_valid',
            'trip_type',
        ]);
        $params['user_id'] = $this->user_id;
        $res               = $this->orderRepo->lists($params,
            $request->input('page_size', 5));
        $res['orders']     = OrderResource::collection($res['orders']);
        return $this->success('', $res);
    }

    /**
     * 订票
     *
     * @param BookTicketRequest $request
     *
     * @return Application|ResponseFactory|Response
     * @throws GuzzleException
     */
    public function book(BookTicketRequest $request)
    {
        $orderData            = $request->all();
        $orderData['user_id'] = $this->user_id;
        Log::info(json_encode(['method' => '订票', 'request' => $request->all()], JSON_UNESCAPED_UNICODE));
        $order_manager = new BookOrdersManager($orderData);
        $orders        = $order_manager->book();
        $order_info    = new TripBookOrderResource($orders[0]);
        return $this->success('', compact('order_info'));
    }

    /**
     * 订票前价格明细
     *
     * @param BookTicketRequest $request
     *
     * @return Application|ResponseFactory|Response
     * @throws GuzzleException
     */
    public function before_book_price_details(BookTicketRequest $request)
    {
        $orderData            = $request->all();
        $orderData['user_id'] = $this->user_id;
        Log::info(json_encode(['method' => '订票前价格明细', 'request' => $request->all()], JSON_UNESCAPED_UNICODE));
        $order_manager = new BookOrdersManager($orderData);
        $res           = $order_manager->before_book_price_details();
        $price_details = $res['price_details'];
        return $this->success('', compact('price_details'));
    }

}
